<?php
/**
 * Created by PhpStorm.
 */

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use app\common\lib\MsgQueue as MQ;
use app\service\MessageQueue as MsgService;

class MessageQueueDelay extends Command
{
    protected function configure()
    {
        $this->setName('MqDelay')->setDescription('延时消息队列消费者');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            $m = new MQ();
            $callback = function ($msg) {
                $queueName = $msg->delivery_info['routing_key'];
                $msgData = json_decode($msg->body, true);
                $msgObj = new MsgService();
                $res = $msgObj->receiveDelayCallBack($queueName, $msgData['body']);
                if ($res['status'] != STATUS_CODE_SUCCESS) {
                    $delayTime = $res['data']['delaySecond'] ?? 0;
                    if ($delayTime > 0) {
                        $m = new MQ();
                        $m->sendMessage($queueName, $msgData['body'], true, $delayTime);
                    }
                }
                $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            };
            $m->receiveMesage([
                MQ::QUEUE_DELAY_NORMAL,
            ], $callback, true,MQ::BIND_QUEUE_DELAYDEFAULT);
        } catch (\Exception $e) {
//            dump($e->getMessage());
        }
    }

}


